import { handleSelectData } from '@src/util/languages.js';
import { isNotEmpty, isNull } from '@src/util/type'

// 物料这些字段兼容国际化需要特殊处理 - 取值读的是datasource 或 datasourceLanguage
export const materialSpecialHandlingFieldName = ['property', 'snManage', 'returnType', 'forSale', 'batchNumManage', 'isPeriodManage', 'shelfLife'];

// 处理物料数据
export function handlerMaterialList(list, fields) {
  return (
    (list || []).map((item) => {
      fields.forEach((zitem) => {
        let isPeriodManageSub = item.isPeriodManage;
        if (materialSpecialHandlingFieldName.includes(zitem.fieldName || zitem.field)) {
          if (zitem.isSystem === 0 && item?.attribute?.[zitem.fieldName]) {
            item.attribute[zitem.fieldName] = handleSelectData(
              zitem,
              item?.attribute?.[zitem.fieldName]
            );
          } else {
            item[zitem.fieldName] = handleSelectData(
              zitem,
              item?.[zitem.fieldName]
            );
          }
        }

        if (zitem.fieldName === 'shelfLife') {
          if (!item['shelfLifeBk']) {
            item['shelfLifeBk'] = item.shelfLife;
          }

          if (!isNull(item?.shelfLifeValue) && isNotEmpty(item?.shelfLife) && isPeriodManageSub) {
            // 取备份的值，防止重复拼接
            item.shelfLife = `${item.shelfLifeValue}${item.shelfLifeBk}`
          } else {
            item.shelfLife = '';
          }
        }
      });
      return item;
    }) || []
  );
}
